package com.osdp.base.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.osdp.common.http.Result;
import com.osdp.common.util.BeansUtil;
import com.osdp.common.util.ConvertUtil;
import com.osdp.base.entity.SysDict;
import com.osdp.base.service.ISysDictService;
import com.osdp.base.vo.SysDictVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

@Api(tags = "数据字典信息")
@RestController
@RequestMapping("/base/dict")
public class SysDictController {

    @Autowired
    private ISysDictService sysDictService;

    @PostMapping("/list")
    public Result<IPage<SysDictVo>> pageList(SysDictVo sysDictVo){
        SysDict sysDict = ConvertUtil.sourceToTarget(sysDictVo, SysDict.class);
        IPage<SysDictVo> result = sysDictService.pageList(sysDict,new Page<>(sysDictVo.getPageNo(), sysDictVo.getPageSize()))
                .convert(track -> BeansUtil.copy(track, new SysDictVo()));
        return Result.success(result);
    }

    @GetMapping("/allList")
    public List<SysDictVo> queryAllList(){
       List<SysDict> list =  sysDictService.queryAllList();
       List<SysDictVo> sysDictList = ConvertUtil.sourceToTarget(list, SysDictVo.class);
        return sysDictList;
    }

    @PostMapping("/insert")
    public boolean insert(@RequestBody SysDictVo params) {
        SysDict sysDict = ConvertUtil.sourceToTarget(params, SysDict.class);
        return sysDictService.insertDict(sysDict);
     }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public boolean update(@RequestBody SysDictVo params) {
//        SysDict sysDict = JSON.parseObject(jsonObject.toJSONString(), SysDict.class);
        SysDict sysDict = ConvertUtil.sourceToTarget(params, SysDict.class);
        return sysDictService.updateDict(sysDict);
    }

    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    public boolean deleteBatch(@RequestParam(name = "ids", required = false) String ids){
        if(StringUtils.isEmpty(ids)) {
            return false;
        }
        return sysDictService.deleteBatcah(Arrays.asList(StringUtils.split(ids, ",")));
     }

    @DeleteMapping(value = "/delete")
    @ApiOperation(value = "删除字典")
    public boolean delete(@RequestParam(name = "id", required = true) String id) {
        if(StringUtils.isEmpty(id)) {
            return false;
        }
        return sysDictService.deleteDict(id);
   }

    @RequestMapping(value = "/queryById", method = RequestMethod.GET)
    public Result<SysDictVo> queryById(@RequestParam(name = "id", required = true) String id) {
        SysDict entity = sysDictService.getById(id);
        SysDictVo sysDictVo = ConvertUtil.sourceToTarget(entity, SysDictVo.class);
        return Result.success(sysDictVo);
    }
}
